﻿using MyFileView.Core.Constant;
using MyFileView.Core.Core;
using PdfiumViewer;
using System.Drawing.Imaging;
using System.Linq;

namespace MyFileView.Core.Service.Pdf
{
    public interface IPdfConvertServie
    {
        void ConvertToPng();
    }
    public class PdfConvertServie(IConvertFileFactory convertFileFactory) : IPdfConvertServie, RegisterAutofac
    {
        public void ConvertToPng()
        {
            var context = convertFileFactory.ConvertFileContext;
            context.AfterConvertPathHandle.PathCollection.Clear();
            string imageSuffix = PathConstant.Point + FileConstant.Png;
            ImageConvertCore(imageSuffix);
        }
        /// <summary>
        /// 转化为图片
        /// </summary>
        /// <param name="imageSuffix"></param>
        private void ImageConvertCore(string imageSuffix)
        {
            var context = convertFileFactory.ConvertFileContext;
            var fileInfo = context.FileInfo;
            using (var document = PdfDocument.Load(fileInfo.OutPutFilePath))
            {          
                var pageCount = document.PageCount;

                for (int i = 0; i < pageCount; i++)
                {
                    var dpi = 70;
                    using (var image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi))
                    {
                        var encoder = ImageCodecInfo.GetImageEncoders()
                            .First(c => c.FormatID == ImageFormat.Png.Guid);
                        var encParams = new EncoderParameters(1);
                        encParams.Param[0] = new EncoderParameter(
                            System.Drawing.Imaging.Encoder.Quality, 10L);
                        string filePath = fileInfo.FilePath + PathConstant.Slash + fileInfo.FileName + "-" + i + imageSuffix;
                        image.Save(filePath, encoder, encParams);//    
                        context.AfterConvertPathHandle.Width = image.Width;
                        context.AfterConvertPathHandle.Height = image.Height;
                        context.AfterConvertPathHandle.SetCollection(filePath, RouteConstant.Picture);
                    }

                }
            }
            /* var context= convertFileFactory.ConvertFileContext; 
             string path = HostingEnvironment.MapPath("~/");
             MagickNET.SetGhostscriptDirectory(path +PathConstant.GS);
             var fileInfo = context.FileInfo;
             // Settings the density to 300 dpi will create an image with a better quality
             //var settings = new MagickReadSettings
             //{
             //    Density = new Density(250),
             //  //  Format=MagickFormat.Png
             //};
             //Density 越高时间处理时间越长
             var images = new MagickImageCollection(fileInfo.OutPutFilePath);
             if (images.Count >= 20)
             {
               //   Task.Run( ()=> { });
                     for (int i = 0; i < images.Count; i++)
                     {

                     //使用异步会导致不能写入文件名称路径
                         string filePath = fileInfo.FilePath + PathConstant.Slash + fileInfo.FileName + "-" + i + imageSuffix;
                         images[i].Write(filePath);
                     images[i].ChannelCount
                     }
                     context.AfterConvertPathHandle.SetCollection(images);
                     images.Clear();


                 return RouteConstant.Picture;
             }
             return RouteConstant.Pdf;
             // Create new image that appends all the pages horizontally
             //using var horizontal = images.AppendHorizontally();

             //// Save result as a png
             //horizontal.Write(fileInfo.FilePath + "/Snakeware.horizontal.png");

             //// Create new image that appends all the pages vertically
             //using var vertical = images.AppendVertically();

             //// Save result as a png
             //vertical.Write(fileInfo.FilePath + "/Snakeware.vertical.png");
            */
        }
    }
}
